package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
/**
 * http://bbs.taobao.com/ 娣樺疂璁哄潧
 * @author abc
 */
public class TaobaoBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"https://[a-z]*.bbs.taobao.com/detail.html",
				"http://bbs.taobao.com/catalog/thread/[0-9]*-[0-9]*.htm",
				"http://bbs.taobao.com/catalog/thread/[0-9]*-[0-9]*.htm[?].*",
				"http[s]?://bbs.taobao.com/catalog/thread/[0-9]*-[0-9]*.htm.*",
				//http://bbs.taobao.com/catalog/thread/16094510-260245438.htm?spm=a210m.7475144.1998166562.19.dcQOui
				};
		//https://dacu.bbs.taobao.com/detail.html?spm=0.0.0.0.3HO9MM&postId=7233997
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;
		String author = null;
		Document doc = Jsoup.parse(htmltxt);
		Element first = doc.select("div.detail-post div.sidebar div.user-info h4 span").first();
		if(first!=null){
			author = first.text();
			bbs.setAuthor(author);
		}
		if(doc.select("div.msg p.error").size()>0){
			bbs.setUpdateUrl(ParseState.ERR404.toString());
			return bbs;
		}

		title =doc.select("li.h3").text();
		if(title==null || title.trim().length()==0){
		}
		//content=doc.select("div.content").html();
		date=DateUtils.matchDate(doc.select("span.time").html());
		content = HtmlCleaner.getContentHtml(url, doc.select("div.content"));
		String tempdate=doc.select("div#content div#detail div.bbd div.detail-post div#reply0.floor div.floor-wrap div.hd span.time-author").text();
		if (tempdate==null || tempdate.trim().length()==0) {
			tempdate=doc.select("span.time").text();
		}
		date = DateUtils.matchDate(tempdate);
        
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setDate(date);

		String clickComentstr=doc.select("div#content div#detail div.detail-title div.visitor").text();
		if(clickComentstr!=null && clickComentstr.trim().length()!=0){
			clickComentstr = clickComentstr.replace(" ", "").replace("|", "");
			if(clickComentstr.indexOf("鏌ョ湅:")!=-1 && clickComentstr.indexOf("鍥炲:")!=-1){
				String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("鏌ョ湅:")+"鏌ョ湅:".length(),clickComentstr.indexOf("鍥炲:"));
				try {
					clickNum=Integer.parseInt(clickNumstr);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			if(clickComentstr.indexOf("鍥炲:")!=-1){
				String commentTemp=clickComentstr.substring(clickComentstr.indexOf("鍥炲:")+"鍥炲:".length());
				try {
					commentNum = Integer.parseInt(commentTemp);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		/**
		 * 瑙ｆ瀽鐢ㄤ簬鏇存柊鐨勫湴鍧�
		 */
		bbs.setUpdateUrl(url);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html = responseToURL.getHtml();
					if(html!=null){
						Integer clickNum=0;
						Integer commentNum=0;
						Document doc = Jsoup.parse(html);
						String clickComentstr=doc.select("div#content div#detail div.detail-title div.visitor").text();
						if(clickComentstr!=null && clickComentstr.trim().length()!=0){
							clickComentstr = clickComentstr.replace(" ", "").replace("|", "");
							if(clickComentstr.indexOf("鏌ョ湅:")!=-1 && clickComentstr.indexOf("鍥炲:")!=-1){
								String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("鏌ョ湅:")+"鏌ョ湅:".length(),clickComentstr.indexOf("鍥炲:"));
								try {
									clickNum=Integer.parseInt(clickNumstr);
								} catch (Exception e) {
									// TODO: handle exception
								}
							}
							if(clickComentstr.indexOf("鍥炲:")!=-1){
								String commentTemp=clickComentstr.substring(clickComentstr.indexOf("鍥炲:")+"鍥炲:".length());
								try {
									commentNum = Integer.parseInt(commentTemp);
								} catch (Exception e) {
									// TODO: handle exception
								}
							}
						}
						meta.setClickNum(clickNum);
						meta.setCommentNum(commentNum);
						return meta;
					}

				}
			}
		}
		return null;
	}
    
	
}
